if (!window.anthill.visualization) anthill.visualization = {};

anthill.visualization.Tree = function (container) {
	this.container = container;
	this.allRows = {};
	this.selectedRows=[];
};

anthill.visualization.Tree.prototype.draw = function (data, options) {
	//data must have id, parent_id, node_text
	var html = document.createElement('div');
	html.setAttribute('class', 'anthill_tree');
	this.drawChildren(html, data, 0);
	this.container.innerHTML = '';
	this.container.appendChild(html);
};

anthill.visualization.Tree.prototype.drawChildren = function (html, data, indx) {
	var rows = data.getFilteredRows([{column: 1, value: String(indx)}]);
	var ctrl = null;
	if(indx != '0' && indx != '' && indx != null) {
		ctrl = document.createElement('div');
		ctrl.setAttribute('class', 'anthill_tree_control');
		html.firstChild.insertBefore(ctrl,html.firstChild.firstChild);
		var ul_class = 'anthill_tree_ul hide';
	} else {
		var ul_class = 'anthill_tree_ul';
	}
	if(rows.length > 0) {
		var ul = document.createElement('ul');	
		if(ctrl) {
			ctrl.innerHTML = '+';
			ctrl.onclick = function(event){
				event.stopPropagation();
				if(this.innerHTML == '+') {
					this.innerHTML = '-';
					ul.setAttribute('class', 'anthill_tree_ul');
				} else {
					this.innerHTML = '+';
					ul.setAttribute('class', 'anthill_tree_ul hide');
				}
			};
		}
		ul.setAttribute('class', ul_class);
		html.appendChild(ul);
		for(var r in rows) {
			var li = document.createElement('li');
			li.setAttribute('class', 'anthill_tree_li');
			ul.appendChild(li);
			div = document.createElement('div');
			div.setAttribute('class', 'anthill_tree_inner_div');
			li.appendChild(div);
			div.innerHTML = data.getFormattedValue(rows[r], 2);
			div.r = rows[r];
			this.allRows[rows[r]] = div;
			div.v = this;
			div.onclick = function(event) {
				event.stopPropagation();
				if(event.ctrlKey){
					var selection = this.v.getSelection();
					var inSel = false;
					for(var i=0; i<selection.length; i++) {
						if(selection[i].row == this.r) {
							selection.splice(i,1);
							inSel = true;
						}
					}
					if(!inSel) {
						selection.push({row: this.r, column: null});
					}
				} else {
					var selection = [{row: this.r, column: null}];
				}
				
				this.v.setSelection(selection);
				google.visualization.events.trigger(this.v, 'select', {});

			};
			this.drawChildren(li, data, data.getFormattedValue(rows[r], 0));
		}
	} 
};

anthill.visualization.Tree.prototype.setSelection = function(sel_array) {
	this.setHighlight(this.selectedRows, false);
	this.setHighlight(sel_array, true);
	this.selectedRows = sel_array;
};

anthill.visualization.Tree.prototype.getSelection = function() {
	var ret = [];
	for(var i = 0; i<this.selectedRows.length; i++) {
		ret.push(this.selectedRows[i]);
	}
	return ret;
};

anthill.visualization.Tree.prototype.setHighlight = function(sel_array, doHlght) {
	for(var i in sel_array) {
		if(doHlght) {
			this.allRows[sel_array[i].row].setAttribute('class', 'anthill_tree_inner_div selected');//style.backgroundColor = '#D6E9F8';
		} else {
			this.allRows[sel_array[i].row].setAttribute('class', 'anthill_tree_inner_div');
		}
	}
};